#!/usr/bin/env python
# coding=utf-8
#
#       ggfx.py      
#       
#       Copyright 2008 Dirk B. <dirkbo@googlemail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
import pygame
import os, sys
from pygame.locals import *

def load_image(name, colorkey=None):
	fullname = os.path.join('data/images', name)
	try:
		image = pygame.image.load(fullname)
	except pygame.error, message:
		print 'Cannot load image:', fullname
		raise SystemExit, message
	image = image.convert()
	if colorkey is not None:
		if colorkey is -1:
			colorkey = image.get_at((0,0))
		image.set_colorkey(colorkey, RLEACCEL)
    	return image, image.get_rect()

class Grafix():
	def __init__(self,bs):
		self.gfx = []
		self.oresx = 1024.0
		self.oresy = 768.0
		self.nresx = float(bs.xres)
		self.nresy = float(bs.yres)
		self.fx = self.nresx / self.oresx
		self.fy = (self.nresx * 0.75 ) / self.oresy
		print "Setze Größenfaktor für Map auf: ", self.fx, "/", self.fy
		#init and scale fonts
		#Font:
		self.font_h1 = pygame.font.Font(None, int(50.0 * self.fy))
		self.menufont_h1 = pygame.font.Font(None, int(50 * self.fy))
		self.menufont_h2 = pygame.font.Font(None, int(40 * self.fy))
		self.menufont_h3 = pygame.font.Font(None, int(30 * self.fy))
		self.scorefont   = pygame.font.Font(None, int(40 * self.fy))
		print "Scorefont: ",(40.0 * self.fy)

	def load(self, name):
		im, rect = load_image(name, (255,0,255))
		imn, nrect = self.scale(im, rect)
		self.gfx.append(imn)
		return rect.width, rect.height
		
	def scale(self, im, rect):
		if(self.nresx != self.oresx):
			nrect = Rect(rect)
			nrect.width = round(self.fx * rect.width)
			nrect.height = round(self.fy * rect.height)
			imn = pygame.Surface( (nrect.width, nrect.height) )
			imn = pygame.transform.scale(im,(nrect.width, nrect.height))
			#print "## Rescaling Graphic ##"
			#print "X-Basis, X-Verwendet, Faktor X-Achse:", self.oresx, " -> ", self.nresx, " : ",self.fx
			#print "Y-Basis, Y-Verwendet, Faktor Y-Achse:", self.oresy, " -> ", self.nresy, " : ",self.fy
			#print "Geladenes Objekt: ", rect.width, "x", rect.height
			#print "Neues Objekt: ", nrect.width, "x", nrect.height
		else:
			nrect = rect
			imn = im
		
		#imn.convert()			
		return imn, nrect
		
	def get(self, id):
		return self.gfx[id]
		
	def get_new(self, id):
		ni = self.gfx[id].copy()
		return ni
		
	def get_len(self):
		try:
			return len(self.gfx)
		except:
			return 0
		
	def unload(self):
		self.gfx = []

class XObjx:
	def __init__(self, name=""):
		#self.imlist = []
		# legtd en namen des Objektes fest
		self.name = name
		#legt den startwert der Bildfolge fest.
		self.firstnormal = 0
		self.lastnormal = 0
		#legt die Bildfolge der Konstruktion fest
		self.firstconstruct = 0
		self.lastconstruct = 0
		#Legt die Bildfolge der Schuttstücke fest
		self.firstpiece = 0
		self.lastpiece = 0
		
	#def addimage(self, image):
	#	self.imlist.append(image)
	
class VHObjx:
	def __init__(self, name=""):
		#self.imlist = []
		# legtd en namen des Objektes fest
		self.name = name
		#legt den startwert der Bildfolge fest.
		self.first = 0
		self.last = 0
		self.originalwidth = -1
		self.originalheight = -1
		